<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')" :close-on-click-modal="false">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px">
			<el-form-item label="上级部门" prop="pid">
				<el-cascader v-model="form.pid" :options="groups" :props="groupsProps" :show-all-levels="false" clearable style="width: 100%;"></el-cascader>
                <!-- <el-select v-model="form.pid"  filterable style="width: 100%" placeholder="请选择上级部门" >
                    <el-option v-for="item in groups" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select> -->
			</el-form-item>
            <el-form-item label="组织" prop="org_id">
                <el-select v-model="form.org_id"  filterable style="width: 100%" placeholder="请选择组织" >
                    <el-option v-for="item in orgList" :key="item.id" :label="item.title" :value="item.id"/>
                </el-select>
			</el-form-item>
			<el-form-item label="部门名称" prop="name">
				<el-input v-model="form.name" placeholder="请输入部门名称" clearable></el-input>
			</el-form-item>
			<el-form-item label="负责人" prop="leader_id">
                <el-select v-model="form.leader_id"  filterable style="width: 100%" placeholder="请选择负责人" clearable>
                    <el-option v-for="item in humList" :key="item.id" :label="item.name" :value="item.id"/>
                </el-select>
			</el-form-item>
			<el-form-item label="联系电话" prop="mobile">
				<el-input v-model="form.mobile" placeholder="请输入联系电话" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="1" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="是否有效" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="form.remark" clearable type="textarea"></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:"",
					pid: "",
					name: "",
					leader_id: "",
                    leader:"",
					mobile: "",
					sort: 1,
					status: 1,
					remark: ""
				},
				//验证规则
				rules: {
					sort: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					label: [
						{required: true, message: '请输入部门名称'}
					]
				},
				//所需数据选项
				groups: [],
                orgList:[],
				groupsProps: {
					value: "id",
					emitPath: false,
					checkStrictly: true
				},
                humList:[],
			}
		},
		mounted() {
			// this.getDept()
            this.getGroup()
            this.getOrg()
            this.getHum()
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//加载树数据
			async getGroup(){
				var res = await this.$API.system.dept.list.get({is_page:0});
				this.groups = res.data;
			},
            // async getDept(){
            //     var res = await this.$API.all.getDep.get({is_page:0});
            //     this.groups = res.data;
            // },
            // 组织
            async getOrg(){
                var res = await this.$API.all.getOrg.get({is_page:0});
                this.orgList = res.data;
            },
            async getHum(){
                var res = await this.$API.all.getAdmin.get({is_page:0});
                this.humList = res.data
            },
			//表单提交方法
			submit(){
                this.humList.map(item=>{
                    if(this.form.leader_id==item.id){
                        this.form.leader = item.name
                    }
                })
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res  = [];
						if (this.form.id) {
							res = await this.$API.system.dept.edit.post(this.form);
						}else{
							res = await this.$API.system.dept.add.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 1){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.id = data.id
				this.form.name = data.name
				this.form.status = data.status
				this.form.sort = data.sort
				this.form.pid = data.pid
				this.form.remark = data.remark
				this.form.leader_id = data.leader_id
				this.form.leader_id = data.leader
				this.form.mobile = data.mobile
				this.form.org_id = data.org_id
			}
		}
	}
</script>

<style>
</style>
